﻿namespace BM.Tools.WebControls.MForm.Controls.Modifiers
{
    using System.Collections.ObjectModel;

    using Items;

    /// <summary>
    /// This class keeps a collection of modifier objects for a given offspring and offspring item. It also attaches specific modifier methods to correpsonding offspring and offspring item event handlers.
    /// </summary>
    /// <typeparam name="TModifier">
    /// The modifier type
    /// </typeparam>
    /// <typeparam name="T">
    /// The offspring type
    /// </typeparam>
    /// <typeparam name="TItem">
    /// the offspring item type.
    /// </typeparam>
    internal class ModifiersCollection<TModifier, T, TItem> : Collection<TModifier>
        where TModifier : Modifier<T, TItem>
        where T : Offspring
        where TItem : OffspringItem
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ModifiersCollection{TModifier,T,TItem}"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        public ModifiersCollection(T parent)
        {
            Parent = parent;
        }

        /// <summary>
        /// Gets the offspring parent.
        /// </summary>
        public T Parent
        {
            get;
            private set;
        }

        /// <summary>
        /// Inserts the modifier to the collection and adds Init, Load and PreRender event handlers
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <param name="item">
        /// The modifier item.
        /// </param>
        protected override void InsertItem(int index, TModifier item)
        {
            base.InsertItem(index, item);
            Parent.Init += item.OnBasisInitInternal;
            Parent.Load += item.OnBasisLoadInternal;
            Parent.PreRender += item.OnBasisPreRenderInternal;
            Parent.ItemCreated += item.OnItemCreatedInternal;
            AddEventsForExistingItems(item);
        }

        /// <summary>
        /// Removes the modifier from the collection and removes the Init, Load and PreRender event handlers
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        protected override void RemoveItem(int index)
        {
            var removedModifier = this[index];
            Parent.Init -= removedModifier.OnBasisInitInternal;
            Parent.Load -= removedModifier.OnBasisLoadInternal;
            Parent.PreRender -= removedModifier.OnBasisPreRenderInternal;
            Parent.ItemCreated -= removedModifier.OnItemCreatedInternal;
            base.RemoveItem(index);
        }

        private void AddEventsForExistingItems(TModifier item)
        {
            if (Parent.HasControls())
            {
                foreach (var offspringItem in Parent.Items)
                {
                    item.OnItemCreatedInternal(this, new OffspringItemEventArgs(offspringItem, offspringItem.Index));
                }
            }
        }
    }
}
